﻿using System.Data;
using System.Data.Common;

namespace Portal.Workflow.Model
{
    public sealed class DatabaseParamCls : DbParameter, IDbDataParameter, IDataParameter
    {
        public DatabaseParamCls() 
        {
            this.DbType = System.Data.DbType.String;
        }

        public DatabaseParamCls(string paramName, object paramValue)
        {
            this.ParameterName = paramName;
            this.Value = paramValue;
            this.DbType = System.Data.DbType.String;
        }

        public DatabaseParamCls(string paramName, DbType paramType, object paramValue)
        {
            this.ParameterName = paramName;
            this.DbType = paramType;
            this.Value = paramValue;
        }

        public DatabaseParamCls(string paramName, DbType paramType, int paramSize, object paramValue)
        {
            this.ParameterName = paramName;
            this.DbType = paramType;
            this.Value = paramValue;
            this.Size = paramSize;
        }

        public string FieldName
        {
            get
            {
                return this.ParameterName;
            }
        }

        public SqlDbType SqlFieldType
        {
            get
            {
                switch (this.DbType)
                {
                    case System.Data.DbType.AnsiString:
                        return SqlDbType.VarChar;
                    case System.Data.DbType.Binary  :
                        return SqlDbType.Image;
                    case System.Data.DbType.Boolean:
                        return SqlDbType.Bit;
                    case System.Data.DbType.Byte:
                        return SqlDbType.TinyInt;
                    case System.Data.DbType.Currency:
                        return SqlDbType.Money;
                    case System.Data.DbType.DateTime2:
                        return SqlDbType.DateTime2;
                    case System.Data.DbType.Date:
                        return SqlDbType.Date;
                    case System.Data.DbType.DateTime:
                        return SqlDbType.DateTime;
                    case System.Data.DbType.Decimal:
                        return SqlDbType.Decimal;
                    case System.Data.DbType.Double:
                        return SqlDbType.Float;
                    case System.Data.DbType.Int16:
                        return SqlDbType.SmallInt;
                    case System.Data.DbType.Int32:
                        return SqlDbType.Int;
                    case System.Data.DbType.Int64:
                        return SqlDbType.BigInt;
                    case System.Data.DbType.String:
                        return SqlDbType.NVarChar;
                    case System.Data.DbType.Guid:
                        return SqlDbType.UniqueIdentifier;
                    case System.Data.DbType.Object:
                        return SqlDbType.Variant;
                    case System.Data.DbType.Time:
                        return SqlDbType.Time;
                    case System.Data.DbType.Xml:
                        return SqlDbType.Xml;
                    case System.Data.DbType.StringFixedLength:
                        return SqlDbType.NChar;
                    case System.Data.DbType.AnsiStringFixedLength:
                        return SqlDbType.Char;
                    default:
                        return SqlDbType.NVarChar;
                }
            }
        }

        private bool allow = true;
        public bool Allow
        {
            get { return allow; }
            set { allow = value; }
        }

        public override object Value { get; set; }
        public override DbType DbType { get; set; }
        public override ParameterDirection Direction { get; set; }
        public override bool IsNullable { get; set; }
        public override string ParameterName { get; set; }
        public override int Size { get; set; }
        public override string SourceColumn { get; set; }
        public override bool SourceColumnNullMapping { get; set; }
        public override DataRowVersion SourceVersion { get; set; }

        public override void ResetDbType()
        {
            this.DbType = System.Data.DbType.String;
        }
    }
}
